package com.stenio.fancat.controller;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.WebApplicationContext;

import com.stenio.fancat.common.constants.GlobalConstant;
import com.stenio.fancat.domain.UserImage;
import com.stenio.fancat.service.MainService;
import com.stenio.fancat.service.UserImageService;
import com.stenio.fancat.util.ResultUtil;
import com.stenio.fancat.util.WebUtil;
import com.stenio.fancat.vo.Result;

/**
 * 首页模块controller
 */
@RestController
@RequestMapping("index")
public class MainController {

	@Autowired
	WebApplicationContext applicationContext;
	@Autowired
	private MainService mainServic;
	@Autowired
	private UserImageService userImageService;

	/**
	 * 获取首页面频道
	 * 
	 * @return
	 */
	@PostMapping("channels")
	public Result queryChannels() {
		return ResultUtil.success(mainServic.queryChannelBrands());
	}

	/**
	 * 非平台点击包装后的活动url执行事件
	 * 
	 * @return
	 */
	@RequestMapping("send/{actId}/{userId}")
	public void toURL(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// response.sendRedirect("http://www.baidu.com");
		response.sendRedirect(mainServic.queryActUrl(request.getRequestURI(), WebUtil.getIpAddress(request)));
	}

	/**
	 * 显示用户头像图片
	 * @param response
	 * @param userId
	 * @throws IOException
	 */
	@RequestMapping("/icon/{userId}")
	public void getImageById(final HttpServletResponse response, @PathVariable(value = "userId") Integer userId)
			throws IOException {
		UserImage userImage = userImageService.queryUserImageByCondition(userId, null);
		response.setContentType("image/jpeg");
		response.setCharacterEncoding(GlobalConstant.PLATFORM_CHARACTER_ENCODING);
//		InputStream in = new ByteArrayInputStream(scaleImage(userImage.getImage(), 128, 128));
		if(null == userImage || null == userImage.getImage()){
			return;
		}
		InputStream in = new ByteArrayInputStream(userImage.getImage());
		int len = 0;
		byte[] buf = new byte[1024];
		OutputStream outputSream = response.getOutputStream();
		while ((len = in.read(buf, 0, 1024)) != -1) {
			outputSream.write(buf, 0, len);
		}
		outputSream.close();
	}

	public static byte[] scaleImage(byte[] data, int width, int height) throws IOException {
		BufferedImage buffered_oldImage =

				ImageIO.read(new ByteArrayInputStream(data));
		int imageOldWidth = buffered_oldImage.getWidth();
		int

		imageOldHeight = buffered_oldImage.getHeight();
		double scale_x = (double) width / imageOldWidth;
		double

		scale_y = (double) height / imageOldHeight;
		double scale_xy = Math.min(scale_x, scale_y);
		int imageNewWidth

				= (int) (imageOldWidth * scale_xy);
		int imageNewHeight = (int) (imageOldHeight * scale_xy);
		BufferedImage

		buffered_newImage = new BufferedImage(imageNewWidth, imageNewHeight, BufferedImage.TYPE_INT_RGB);

		buffered_newImage.getGraphics().drawImage(
				buffered_oldImage.getScaledInstance(imageNewWidth, imageNewHeight, BufferedImage.SCALE_SMOOTH), 0,

				0, null);
		buffered_newImage.getGraphics().dispose();
		ByteArrayOutputStream outPutStream = new

		ByteArrayOutputStream();
		ImageIO.write(buffered_newImage, "jpeg", outPutStream);
		return

		outPutStream.toByteArray();
	}
}
